package de.hauck.rtmidlet.utils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockStore;
import de.hauck.rtmidlet.views.FormSettingsMenu;

public class MyLoggerFSWriterThread implements Runnable {

	private String _message;
	//private boolean _fileWritingPossible;
	private static final String LogFileName = "RTMidlet.txt";

	public MyLoggerFSWriterThread(String message) {
		// _myStore = myStore;
		// _main=rtmidlet;
		// _fm = fm;
		_message = message;
		//_fileWritingPossible = fileWritingPossible;
		new Thread(this).start();
	}

	public void run() {
		try {
			FileConnection logConnection = null;
			// String exportFolder = "Speicherkarte/";
			// //Controller.getController().getSettings()
			// .getExportFolder();
			// fullPath = "file:///" + exportFolder + LogFileName;
			String fullPath = "file:///" + Configuration.exportPath
					+ LogFileName;
			DataOutputStream streamOut = null;

			try {
				// ------------------------------------------------------------------
				// Create a FileConnection and if this is a new stream create
				// the
				// file
				// ------------------------------------------------------------------

				// Logger.debug("FILE: path is " + fullPath);
				if (logConnection == null) {
					try {
						logConnection = (FileConnection) Connector
								.open(fullPath);
						// Create file
						if (logConnection != null && !logConnection.exists()) {
							logConnection.create();
						} else {
							// File already exists: zero the contents so we get
							// a new log each time
							// logConnection.delete();
							logConnection.create();
						}
					} catch (IOException e) {
						Configuration.fileWritingPossible = false;
						MyLogger.debug("WriteLog failed to create connection:"
								+ fullPath + ":" + e.getMessage());
						logConnection = null;
					}
				}

				if (logConnection != null && streamOut == null) { // XXX:
																	// streamOut
																	// is always
																	// null
																	// here?
					// open the stream at the end so we can append to the file
					System.out.println("Filesize:" + logConnection.fileSize());
					OutputStream x = logConnection
							.openOutputStream(logConnection.fileSize());
					streamOut = new DataOutputStream(x);
				}

				if (streamOut != null) {
					streamOut.write(((String) _message + "\n").getBytes());
					streamOut.flush();
					streamOut.close();
					streamOut = null;
				} else {
					Configuration.fileWritingPossible = false;
					System.out.println("Logger: output stream is null");
				}
			} catch (IOException e) {
				Configuration.fileWritingPossible = false;
				System.out.println("Logger: error:" + e.getMessage());
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
